<?php
/**
 * XmuSlh, web platform of Student Association Federation of Xiamen University
 * 
 * Rewritting one or some pages of StatusNet. 
 * To get the original version, please visit <http://status.net/>.
 * 
 * StatusNet, the distributed open-source microblogging tool
 * 
 * LICENCE: This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/**
 * The base deletion action.
 * Including a 'Aresureform' and 'Returnto' solution.
 * 
 * @author		ytukie <ytukie@gmail.com>
 * @version		0.2
 * 
 * 0.2)  Adds some logging action while deleting.
 */

// XmuSlh only.
if ( !defined('XMUSLH')) { exit(1); }

//Requirments.
require_once INSTALLDIR . '/lib/slh_action.php';
require_once INSTALLDIR . '/lib/slh_util.php';


class DeleteAction extends Action
{
	/**
	 * The name of the deleting item.
	 */
	var $itemname;
	
	/**
	 * The argument flag of the deleting item's id.
	 */
	var $itemidarg;
	
	/**
	 * The deleting sub-action.
	 */
	var $action;
	
	/**
	 * The id key of the item.
	 */
	var $itemid;
	
	/**
	 * Constructor.
	 * To gain some vairables of the sub-action.
	 */
	function __construct($itemname,
						 $itemidarg,
						 $action)
	{
		parent::__construct();
		
		$this->itemname = $itemname;
		$this->itemidarg = $itemidarg;
		$this->action = $action;
	}
	
	/**
	 * Overriding.
	 */
	function title()
	{
		return ('Deletion.');
	}
	
	function prepare($args)
	{
		parent::prepare($args);
		
		// Get the item id.
		$this->itemid = $this->getTrimmedArg($this->itemidarg);
		
		if (empty($this->itemid)) {
			$this->clientError('没有找到相应的' . $this->itemname . '哦,亲!');
		}

		return true;
	}
	
	function handle($args)
	{
		if ($this->getArg('no')) {
			$this->returnTo();
		} else if ($this->getArg('yes')) {
			// CSRF protection and Refreshing-post protection.
			$this->checkSessionToken();
			
			slh_log_deletion(slh_get_usertitle() . ' 删除了' . $this->itemname . '[' . $this->itemid . ']');
			
			$this->handleDelete();
			$this->returnTo();
		} else {
			$this->showPage();
		}
	}
	
	/**
	 * Overrideing the showInnerContent().
	 */
	function showInnerContent()
	{
		$this->areYouSureForm();
	}
	
// Overrided ends //////////////////////////////////////////////////////////////	
	/**
	 * Return to the former page according the SESSION.
	 */
	function returnTo()
	{
		$url = slh_get_returnto();
		if ( !is_null($url)) {
			// Clear it up.
			slh_clear_returnto();
			slh_redirect($url, 303);
		} else {
			$this->returnToDefault();
		}
	}
	
	/**
	 * The default returnto while the 'returnto' is not given.
	 * Overriding.
	 */
	function returnToDefault()
	{
		slh_redirect(slh_action2url('publicnews'), 303);
	}
	
	/**
	 * Confirm with the user.
	 * 
	 * Shows a confirmation form.
	 */
	function areYouSureForm()
	{
		$this->elementStart('div', 'cls_div_areyousureform');
		
		$this->element('h4', null, '确认操作');
		$this->elementStart('form', array('class' => 'cls_form_dialog',
										  'method'=> 'post',
										  'action'=> slh_action2url($this->action, 
																	array($this->itemidarg => $this->itemid))));
		$this->elementStart('fieldset');
		$this->element('legend', '确认删除');
		
		$this->hidden('token', slh_session_token());
		$this->element('p', array('class' => 'cls_p_instruction'),
					   '亲,您确定要删除这个' . $this->itemname . '吗?');
		
		$this->submit('submit_no',
					  '否',
					  null,
					  'no',
					  '不执行删除操作.');
		$this->submit('submit_yes',
					  '是',
					  null,
					  'yes',
					  '把它删掉!');			  
		
		$this->elementEnd('fieldset');
		$this->elementEnd('form');
		$this->elementEnd('div');
	}
	
	/**
	 * Actually delete a micro news.
	 * Overriding.
	 */
	function handleDelete()
	{
	}
}

?>